<?php
 
class PluginYii_ModuleYii_EntityTopicRead 
extends PluginYii_Inherit_ModuleTopic_EntityTopicRead 
{
	
	protected $_app;
	protected $_ar;
	
	public function __construct($data=array()){
		parent::__construct($data);
		$this->InitYiiApp();
	}
	
	protected function InitYiiApp(){
		if(!$this->_app){
			$this->_app = $this->PluginYii_Yii_App();
		}
	}
	
	public function getAr(){
		if(!($ar = $this->_ar)){
			return LsyiiEntityHelper::getAr($this, false);
		}
		return $ar;
	}
	
	public function setAr(CActiveRecord $ar){
		$this->_ar = $ar;
		return $this;
	}
	
	public function getArModel(){
		return LsyiiEntityHelper::getModel($this);
	}
	
	public function getRelated($relationName, $refresh=false, $params=array()){
		return LsyiiEntityHelper::getRelated($this, $relationName, $refresh, $params);
	}
	
	public function getRelatedDataProvider($relationName, $config=array(), $queryCriteria=array()){
		return $this->getAr()->ardp->getRelatedDataProvider($relationName, $config, $queryCriteria);
	}
	
	
	public function _setData($data){
		if(is_array($data) && isset($data['ar'])){
			$ar = $data['ar'];
			$this->setAr($ar);
			unset($data['ar']);
		}
		return parent::_setData($data);
	}
	
	/*
	 * Overloading
	 */
	
	public function __sleep(){
		$parent = get_parent_class(__CLASS__);
		if(method_exists($parent, '__sleep')){
			$names = call_user_func(array($parent,'__sleep'));//$parent::__sleep();
			foreach($names as $i => $name){
				if(in_array($name, array('_app', '_ar'))){
					unset($names[$i]);
				}
			}
			return $names;
		}
		$vars = get_object_vars($this);
		$names = array();
		foreach($vars as $name => $value){
			if($value instanceof CComponent){
				continue;
			}
			$names[] = $name;
		}
		return $names;
	}
	
	public function __wakeup(){
		$parent = get_parent_class(__CLASS__);
		if(method_exists($parent, '__wakeup')){
			call_user_func(array($parent,'__wakeup'));//$parent::__wakeup();
		}
		$this->InitYiiApp();
	}
	
	public function __get($name){
		if(method_exists($this, $method = 'get'.ucfirst($name))){
			return $this->$method();
		}
		if($model = $this->getArModel()){
			$metaData = $model->getMetaData();
			if($metaData->hasRelation($name)){
				return $this->getRelated($name);
			}
		}
		return parent::__get($name);
	}
	
	public function __call($method, $params){
		// getter
		if(preg_match('#^get(.*)$#i', $method, $t)){
			$name = $t[1];
			$name[0] = strtolower($name[0]);
			if($model = $this->getArModel()){
				$metaData = $model->getMetaData();
				if($metaData->hasRelation($name)){
					array_unshift($params, $name);
					return call_user_func_array(
						array($this, 'getRelated'),
						$params
					);
				}
			}
		}
		// operation
		if(in_array(strtolower($method), array('save','saveattributes','update','insert','delete','refresh'))){
			if($ar = $this->getAr()){
				$ar->setAttributes($this->_getData(), false);
				$result = call_user_func_array(array($ar, $method), $params);
				if($result !== false){
					$this->_setData($ar->getAttributes());
				}
				return $result;
			}
		}
		return parent::__call($method, $params);
	}
	
	/*
	 * Relations
	 */
	
	/*
	 * AR Getters
	 */
	
}